package com.uprizer.free.paramopt;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Random;

import junit.framework.TestCase;

public class ParameterOptimizerTest extends TestCase {

	DecimalFormat df = new DecimalFormat("0.00");
	
	Random random = new Random();
	
	public void testMystery() {
		// Create our ParameterOptomizer object and tell it that valid inputs
		// are in the range 0 to 3
		ParameterOptimizer po = new ParameterOptimizer(0, 3);
		// Output the initial best guess.  Since we don't have any samples yet,
		// this will be mid-way between 0 and 3
		System.out.println(" Best guess: "+df.format(po.getBestGuess()));
		// Now we run a few tests
		for (int x = 0; x < 7; x++) {
			// Get three sample points from the ParameterOptomizer
			List<Double> probes = po.getNextSamples(3);
			for (Double probe : probes) {
				// For each of these points, obtain the output provided by the mysteryFunction
				// Note that in real-life scenarios, this might be a time-consuming process
				double output = mysteryFunction(probe);
				// Tell the ParameterOptomizer what we learned
				po.addSample(probe, output);
				// And output the information so we can see what is going on
				System.out.print(" "+df.format(probe)+":"+df.format(output));
			}
			// Find out the current best guess, along with what we predict the output will be
			// at this point
			System.out.println(" Best guess: "+df.format(po.getBestGuess())+":"+df.format(po.bestFit.getYAt(po.getBestGuess())));
		}
	}
	
	private double mysteryFunction(double input) {
		return Math.sin(input+4)+1.1+(random.nextDouble()/3-0.11);
	}
}
